package com.hankol.material.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.baselibrary.manager.SensorsDataManager;
import com.baselibrary.utils.GlideUtils;
import com.baselibrary.utils.NetworkUtil;
import com.baselibrary.utils.NumberUtils;
import com.baselibrary.utils.OnClickListener;
import com.baselibrary.utils.ToastUtils;
import com.hankol.material.CollectionFolderActivity;
import com.hankol.material.MaterialFilterActivity;
import com.hankol.material.MaterialSearchActivity;
import com.hankol.material.R;
import com.hankol.material.adapter.ContentListVideoAdapter;
import com.hankol.material.api.MaterialConstants;
import com.hankol.material.bean.MaterialContentInfo;
import com.hankol.material.databinding.ActivityMaterialVideoBinding;
import com.hankol.material.rotate.RotateManagerHandler;
import com.tencent.rtmp.ITXVodPlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.Objects;

/**
 * @description: 素材库视频列表View
 * @author: 关忆北丶
 * @date :   2021/11/18 13:19
 */
public class HKPlayVideo extends RelativeLayout implements SeekBar.OnSeekBarChangeListener {
    private Context mContext;
    private TXVodPlayer mVodPlayer = null;
    private String videoUrl = "";
    private int mSlidePro;
    int LIST_UPDATEPLAYER = 0x101;
    int POP_UPDATEPLAYER = 0x102;
    private boolean voice = true; //有声音
    private ActivityMaterialVideoBinding mBinding;
    private PopupWindow mPopupWindow;
    private Activity mAct;
    RotateManagerHandler mRotateManagerHandler = null;
    private Handler tHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {

            if (message.what == LIST_UPDATEPLAYER) {
                mRlMaterialPlayIng.setVisibility(View.GONE);
                mTvMaterialTime.setVisibility(GONE);
                mIvMaterialPlay.setVisibility(View.GONE);
            } else {
                if (mBinding != null)
                    mBinding.ivMaterialVideoSuspend.setVisibility(View.GONE);
            }
            return false;
        }
    });
    private View inflate;

    public void setVideoUrl(String videoUrl, Activity mAct) {
        this.videoUrl = videoUrl;
        this.mAct = mAct;
    }

    public TXVodPlayer getTXVodPlayer() {
        return mVodPlayer;
    }

    public void setNullTXVodPlayer() {
        mVodPlayer = null;
    }

    public HKPlayVideo(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public HKPlayVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public HKPlayVideo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private TXCloudVideoView mTxcMaterial;
    private ImageView mIvMaterialCover;
    private TextView mTvMaterialTitle;
    private ImageView mIvMaterialPlay;
    private ProgressBar mPbMaterial;
    private TextView mTvMaterialTime;
    private TextView mTvMaterialFlowPlay;
    private LinearLayout mLlMaterialFlowTip;
    private TextView mTvMaterialSpeed;
    private TextView mTvMaterialTotalSpeed;
    private SeekBar mSbMaterialSpeed;
    private LinearLayout mLlMaterialSpeed;
    private ImageView mIvMaterialVoice;
    private ImageView mIvMaterialEnlarge;
    private RelativeLayout mRlMaterialPlayIng;
    private RelativeLayout mRlMaterial;
    private MaterialContentInfo.FileLibsBean.DataBean bean;

    private void init() {
        inflate = LayoutInflater.from(mContext).inflate(R.layout.view_hk_video, this);

        mRlMaterial = inflate.findViewById(R.id.rl_material);
        mTxcMaterial = inflate.findViewById(R.id.txc_material);
        mIvMaterialCover = inflate.findViewById(R.id.iv_material_cover);
        mTvMaterialTitle = inflate.findViewById(R.id.tv_material_title);
        mIvMaterialPlay = inflate.findViewById(R.id.iv_material_play);
        mPbMaterial = inflate.findViewById(R.id.pb_material);
        mTvMaterialTime = inflate.findViewById(R.id.tv_material_time);
        mLlMaterialFlowTip = inflate.findViewById(R.id.ll_material_flowTip);
        mTvMaterialFlowPlay = inflate.findViewById(R.id.tv_material_flowPlay);
        mRlMaterialPlayIng = inflate.findViewById(R.id.rl_material_playIng);
        mTvMaterialSpeed = inflate.findViewById(R.id.tv_material_speed);
        mTvMaterialTotalSpeed = inflate.findViewById(R.id.tv_material_totalSpeed);
        mLlMaterialSpeed = inflate.findViewById(R.id.ll_material_speed);
        mSbMaterialSpeed = inflate.findViewById(R.id.sb_material_speed);
        mIvMaterialVoice = inflate.findViewById(R.id.iv_material_voice);
        mIvMaterialEnlarge = inflate.findViewById(R.id.iv_material_enlarge);
        mIvMaterialVoice.setSelected(true);
    }

    public void setVideoTitle(String content) {
        mTvMaterialTitle.setText(content);
    }

    public void setVideoTitle(SpannableString spannableString) {
        mTvMaterialTitle.setText(spannableString);
    }

    public void setCover(MaterialContentInfo.FileLibsBean.DataBean bean) {
        this.bean = bean;
        GlideUtils.loadImg_228(mContext, bean.getThumbnail(), mIvMaterialCover);
    }

    public TXCloudVideoView getTxcView() {
        return mTxcMaterial;
    }

    public void setVideoTime(long videoTime) {
        mTvMaterialTime.setText(NumberUtils.stringForTime(videoTime));
    }

    @SuppressLint("ClickableViewAccessibility")
    public void initListener() {
        mIvMaterialEnlarge.setOnClickListener(view -> {
            if (mVodPlayer != null) {
                mVodPlayer.pause();
            }
            startPopPlay();
            mIvMaterialPlay.setImageResource(R.drawable.ic_material_play_stop);
        });

        mIvMaterialVoice.setOnClickListener(view -> {
            if (mVodPlayer != null) {
                if (mIvMaterialVoice.isSelected()) {
                    //关闭声音
                    voice = false;
                    mVodPlayer.setMute(true);//静音设置
                    mIvMaterialVoice.setImageResource(R.drawable.ic_material_voice_false);
                } else {
                    //打开声音
                    voice = true;
                    mVodPlayer.setMute(false);//有音设置
                    mIvMaterialVoice.setImageResource(R.drawable.ic_material_voice_true);
                }
                mIvMaterialVoice.setSelected(!mIvMaterialVoice.isSelected());
            }
        });

        mRlMaterial.setOnClickListener(view -> {
            if (mVodPlayer != null) {
                if (mVodPlayer.getCurrentPlaybackTime() > 0) {
                    //视频处于播放中，点击视频区域，显示隐藏组件
                    if (mIvMaterialPlay.getVisibility() == View.GONE) {
                        if (mVodPlayer.isPlaying()) {
                            mTvMaterialTime.setVisibility(GONE);
                            mIvMaterialPlay.setVisibility(View.VISIBLE);
                            mRlMaterialPlayIng.setVisibility(View.VISIBLE);
                            tHandler.removeMessages(LIST_UPDATEPLAYER);
                            tHandler.sendEmptyMessageDelayed(LIST_UPDATEPLAYER, 3 * 1000);
                        }
                    } else {
                        if (mVodPlayer.isPlaying()) {
                            mRlMaterialPlayIng.setVisibility(GONE);
                            mIvMaterialPlay.setVisibility(GONE);
                            mTvMaterialTime.setVisibility(GONE);
                        }
                    }
                }

            }
        });

        mSbMaterialSpeed.setOnSeekBarChangeListener(this);
        mLlMaterialSpeed.setOnTouchListener((v, event) -> {
            Rect seekRect = new Rect();
            mSbMaterialSpeed.getHitRect(seekRect);

            if ((event.getY() >= (seekRect.top - 50)) && (event.getY() <= (seekRect.bottom + 50))) {

                float y = seekRect.top + seekRect.height() / 2;
                float x = event.getX() - seekRect.left;
                if (x < 0) {
                    x = 0;
                } else if (x > seekRect.width()) {
                    x = seekRect.width();
                }
                MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(),
                        event.getAction(), x, y, event.getMetaState());
                return mSbMaterialSpeed.onTouchEvent(me);

            }
            return false;

        });
    }

    /**
     * @param listener
     */
    public void setPlayListener(ContentListVideoAdapter adapter, OnItemPlayClickListener listener, com.baselibrary.utils.OnClickListener onClickListener) {
        mIvMaterialPlay.setOnClickListener(view -> {
            if (mVodPlayer != null) {
                if (mVodPlayer.isPlaying()) {
                    setVideoPlaySate(false);
                } else {
                    if (mVodPlayer.getCurrentPlaybackTime() == 0) {
                        //说明是播放完毕之后再次播放
                        if (onClickListener != null) onClickListener.onClick(view, "");
                    }
                    setVideoPlaySate(true);
                }
            } else {
                if (adapter.getNetState() || NetworkUtil.isWifiConnected(mContext)) {
                    mPbMaterial.setVisibility(VISIBLE);
                    startVideo(videoUrl, adapter);
                    if (onClickListener != null) onClickListener.onClick(view, "");
                } else {
                    mLlMaterialFlowTip.setVisibility(VISIBLE);
                    mIvMaterialPlay.setVisibility(GONE);
                }
            }
            extracted(adapter);
            if (listener != null) listener.onPlayClickListener(view);
        });

    }

    private void extracted(ContentListVideoAdapter adapter) {
        if (adapter.getMLastVodPlayer() != null && adapter.getMLastViewHkVideoBinding() != null && mVodPlayer != null) {
            if (!adapter.getMLastVodPlayer().toString().equals(mVodPlayer.toString())) {
                adapter.getMLastViewHkVideoBinding().findViewById(R.id.pb_material).setVisibility(GONE);
                adapter.getMLastViewHkVideoBinding().findViewById(R.id.rl_material_playIng).setVisibility(GONE);
                adapter.getMLastViewHkVideoBinding().findViewById(R.id.iv_material_play).setVisibility(VISIBLE);
                adapter.getMLastViewHkVideoBinding().findViewById(R.id.tv_material_time).setVisibility(VISIBLE);
                ImageView iv_material_play = adapter.getMLastViewHkVideoBinding().findViewById(R.id.iv_material_play);
                iv_material_play.setImageResource(R.drawable.ic_material_play_stop);
                if (adapter.getMLastVodPlayer().isPlaying()) {
                    adapter.getMLastVodPlayer().pause();
                } else {
                    adapter.getMLastVodPlayer().stopPlay(true);
                    adapter.setMLastVodPlayer(null);
                    Objects.requireNonNull(adapter.getMHKPlayVideo()).setNullTXVodPlayer();
                }
                if (adapter.getTHandler() != null) {
                    adapter.getTHandler().removeMessages(LIST_UPDATEPLAYER);
                }

            }
        }

        if (mVodPlayer != null) {
            adapter.setTHandler(tHandler);
            adapter.setMLastVodPlayer(mVodPlayer);
            adapter.setMLastViewHkVideoBinding(inflate);
        }
    }

    /**
     * 点击 继续播放【数据流量情况下出发】
     */
    public void continuedPlay(ContentListVideoAdapter adapter, OnClickListener listener, OnItemPlayClickListener playClickListener, com.baselibrary.utils.OnClickListener onClickListener) {
        mTvMaterialFlowPlay.setOnClickListener(view -> {

            mPbMaterial.setVisibility(VISIBLE);
            startVideo(videoUrl, adapter);
            if (onClickListener != null) onClickListener.onClick(view, "");
            if (adapter.getMLastVodPlayer() != null && adapter.getMLastViewHkVideoBinding() != null) {
                if (!adapter.getMLastVodPlayer().toString().equals(mVodPlayer.toString())) {
                    adapter.getMLastViewHkVideoBinding().findViewById(R.id.rl_material_playIng).setVisibility(GONE);
                    adapter.getMLastViewHkVideoBinding().findViewById(R.id.iv_material_play).setVisibility(VISIBLE);
                    adapter.getMLastViewHkVideoBinding().findViewById(R.id.tv_material_time).setVisibility(VISIBLE);
                    ImageView iv_material_play = adapter.getMLastViewHkVideoBinding().findViewById(R.id.iv_material_play);
                    iv_material_play.setImageResource(R.drawable.ic_material_play_stop);
                    adapter.getMLastVodPlayer().pause();
                    if (adapter.getTHandler() != null) {
                        adapter.getTHandler().removeMessages(LIST_UPDATEPLAYER);
                    }
                }
            }
            adapter.setTHandler(tHandler);
            adapter.setMLastVodPlayer(mVodPlayer);
            adapter.setMLastViewHkVideoBinding(inflate);
            if (playClickListener != null) playClickListener.onPlayClickListener(view);
            if (listener != null) listener.onClick(view);
        });
    }


    /**
     * 视频播放/暂停  设置元素状态
     *
     * @param b true = 播放
     */
    public void setVideoPlaySate(boolean b) {
        if (mVodPlayer != null) {
            if (b) {
                mTvMaterialTime.setVisibility(GONE);
                mIvMaterialPlay.setVisibility(View.VISIBLE);
                mRlMaterialPlayIng.setVisibility(View.VISIBLE);
                mVodPlayer.resume();
                mIvMaterialPlay.setImageResource(R.drawable.ic_material_play_ing);

                tHandler.removeMessages(LIST_UPDATEPLAYER);
                tHandler.sendEmptyMessageDelayed(LIST_UPDATEPLAYER, 3 * 1000);
            } else {
                mIvMaterialPlay.setVisibility(VISIBLE);
                mRlMaterialPlayIng.setVisibility(GONE);
                mTvMaterialTime.setVisibility(VISIBLE);
                tHandler.removeMessages(LIST_UPDATEPLAYER);
                mVodPlayer.pause();
                mIvMaterialPlay.setVisibility(VISIBLE);
                mIvMaterialPlay.setImageResource(R.drawable.ic_material_play_stop);
            }
        }
    }


    public void deactivate() {
        if (mVodPlayer != null) {
            setVideoPlaySate(false);
        }
    }

    private void startVideo(String url, ContentListVideoAdapter adapter) {
        if (TextUtils.isEmpty(url)) {
            ToastUtils.showShort(mContext, "视频链接出错");
            return;
        }

        mTxcMaterial.setLogMargin(12f, 12f, 110f, 60f);
        mTxcMaterial.showLog(false);
        mTxcMaterial.setKeepScreenOn(true);
        mVodPlayer = new TXVodPlayer(mContext);
        mVodPlayer.setVodListener(new ITXVodPlayListener() {
            @Override
            public void onPlayEvent(TXVodPlayer txVodPlayer, int event, Bundle bundle) {
                switch (event) {
                    case TXLiveConstants.PLAY_EVT_VOD_PLAY_PREPARED:
                        //视频播放开始
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            //弹窗播放情况下
                            mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_ing);
                            mBinding.ivMaterialVideoVoice.setImageResource(voice ? R.drawable.ic_material_voice_true : R.drawable.ic_material_voice_false);
                            hidePlayerView();
                        } else {
                            mIvMaterialCover.setVisibility(GONE);
                            mLlMaterialFlowTip.setVisibility(GONE);
                            mRlMaterialPlayIng.setVisibility(VISIBLE);
                            mTvMaterialTime.setVisibility(GONE);
                            mPbMaterial.setVisibility(GONE);
                            setVideoPlaySate(true);
                        }
                        break;
                    case TXLiveConstants.PLAY_EVT_PLAY_END:
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            mVodPlayer.seek(0);
                            mVodPlayer.resume();
                        } else {
                            mVodPlayer.seek(0);
                            setVideoPlaySate(false);
                        }
                        break;
                    case TXLiveConstants.PLAY_EVT_PLAY_LOADING:
                    case TXLiveConstants.PLAY_ERR_NET_DISCONNECT:
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            //弹窗播放情况下
                            mBinding.pbMaterialVideoLoading.setVisibility(View.VISIBLE);
                        } else {
                            //视频流加载loading
                            mPbMaterial.setVisibility(VISIBLE);
                        }
                        break;
                    case TXLiveConstants.PLAY_EVT_VOD_LOADING_END:
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            //弹窗播放情况下
                            mBinding.pbMaterialVideoLoading.setVisibility(View.GONE);
                        } else {
                            //视频流加载loading结束
                            mPbMaterial.setVisibility(GONE);
                        }
                        break;
                    case TXLiveConstants.PLAY_EVT_PLAY_PROGRESS:
                        int progress = bundle.getInt(TXLiveConstants.EVT_PLAY_PROGRESS_MS);
                        int duration = bundle.getInt(TXLiveConstants.EVT_PLAY_DURATION_MS);
                        if (mPopupWindow != null && mPopupWindow.isShowing()) {
                            //弹窗播放情况下
                            if (mBinding.pbMaterialVideoLoading.getVisibility() == View.VISIBLE) {
                                mBinding.pbMaterialVideoLoading.setVisibility(View.GONE);
                            }
                            mBinding.sbMaterialVideoSpeed.setProgress(progress);
                            mBinding.sbMaterialVideoSpeed.setMax(duration);
                            mBinding.tvMaterialVideoTotalSpeed.setText("/" + NumberUtils.stringForTime(duration));
                            mBinding.tvMaterialVideoSpeed.setText(NumberUtils.stringForTime(progress));
                        } else {
                            if (mSbMaterialSpeed.getMax() != duration) {
                                // extracted(adapter);
                                //只設置一次即可
                                mTvMaterialTotalSpeed.setText("/" + NumberUtils.stringForTime(duration));
                                mSbMaterialSpeed.setMax(duration);
                            }

                            mTvMaterialSpeed.setText(NumberUtils.stringForTime(progress));
                            mSbMaterialSpeed.setProgress(progress);
                        }
                        break;

                }
                if (event < 0)
                    // 播放点播文件失败
                    mVodPlayer.stopPlay(true);
            }

            @Override
            public void onNetStatus(TXVodPlayer txVodPlayer, Bundle bundle) {

            }
        });
        mVodPlayer.setPlayerView(mTxcMaterial);
        mVodPlayer.setAutoPlay(true);
        mVodPlayer.enableHardwareDecode(false); // 是否使用硬解码
        mVodPlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_PORTRAIT);// player 渲染角度
        //        TXLiveConstants.RENDER_MODE_FULL_FILL_SCREEN 铺满全屏不留黑边
        //        TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION 将图像等比缩放，适配最长边，缩放后的宽和高都不会超过显示区域，居中显示，存在黑边风险
        mVodPlayer.setRenderMode(TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION);
        mVodPlayer.startVodPlay(url);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        mSlidePro = seekBar.getProgress();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        //手指抬起获取当前SeekBar进度
        if (mVodPlayer != null) {
            mVodPlayer.seek(mSlidePro / 1000);
            setVideoPlaySate(true);
        }
    }

    public void onDestroy() {
        if (mVodPlayer != null) {
            mVodPlayer.pause();
            mVodPlayer.stopPlay(true);
        }
    }


    public interface OnItemPlayClickListener {
        void onPlayClickListener(View view);
    }

    @SuppressLint("SourceLockedOrientationActivity")
    private void startPopPlay() {
        SensorsDataManager.Companion.getInstance().onCreateTime();
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.activity_material_video, null, false);
        mPopupWindow = new PopupWindow(mBinding.getRoot(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mBinding.txclMaterialVideo.setKeepScreenOn(true);
        mVodPlayer.setPlayerView(mBinding.txclMaterialVideo);
        mVodPlayer.resume();

        if (mAct instanceof MaterialFilterActivity) {
            mRotateManagerHandler = new RotateManagerHandler((MaterialFilterActivity) mAct);
        } else if (mAct instanceof CollectionFolderActivity) {
            mRotateManagerHandler = new RotateManagerHandler((CollectionFolderActivity) mAct);
        } else if (mAct instanceof MaterialSearchActivity) {
            mRotateManagerHandler = new RotateManagerHandler((MaterialSearchActivity) mAct);
        }
        if (mRotateManagerHandler != null) mRotateManagerHandler.registerListener();
        mPopupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_ing);
        mBinding.ivMaterialVideoVoice.setImageResource(voice ? R.drawable.ic_material_voice_true : R.drawable.ic_material_voice_false);
        mBinding.ivMaterialVideoSuspend.setSelected(true);
        mPopupWindow.setOnDismissListener(() -> {
            if (mRotateManagerHandler != null) mRotateManagerHandler.unregisterListener();
            mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_stop);
            mBinding.ivMaterialVideoSuspend.setVisibility(View.VISIBLE);
            mVodPlayer.pause();
            mVodPlayer.setPlayerView(mTxcMaterial);
            setVideoPlaySate(false);
            mAct.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            SensorsDataManager.Companion.getInstance().materialLook(bean.getName()
                    , MaterialConstants.MATERIAL_VIDEO
                    , SensorsDataManager.Companion.getInstance().getBrowseTime());
        });
        mBinding.sbMaterialVideoSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mSlidePro = seekBar.getProgress();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (mVodPlayer != null)
                    mVodPlayer.pause();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //手指抬起获取当前SeekBar进度
                if (mVodPlayer != null) {
                    mVodPlayer.seek(mSlidePro / 1000);
                    mVodPlayer.resume();
                    mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_ing);
                    mBinding.ivMaterialVideoSuspend.setSelected(true);
                    hidePlayerView();
                }
            }
        });
        mBinding.llMaterialVideoSpeed.setOnTouchListener((v, event) -> {
            Rect seekRect = new Rect();
            mBinding.sbMaterialVideoSpeed.getHitRect(seekRect);

            if ((event.getY() >= (seekRect.top - 50)) && (event.getY() <= (seekRect.bottom + 50))) {

                float y = seekRect.top + seekRect.height() / 2;
                float x = event.getX() - seekRect.left;
                if (x < 0) {
                    x = 0;
                } else if (x > seekRect.width()) {
                    x = seekRect.width();
                }
                MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(),
                        event.getAction(), x, y, event.getMetaState());
                return mBinding.sbMaterialVideoSpeed.onTouchEvent(me);

            }
            return false;

        });
        mBinding.ivMaterialVideoNarrow.setOnClickListener(view -> {
            mPopupWindow.dismiss();
        });
        mBinding.ivMaterialVideoVoice.setOnClickListener(view -> {
            if (voice) {
                mVodPlayer.setMute(true);//静音设置
                mBinding.ivMaterialVideoVoice.setImageResource(R.drawable.ic_material_voice_false);
            } else {
                mVodPlayer.setMute(false);//有声设置
                mBinding.ivMaterialVideoVoice.setImageResource(R.drawable.ic_material_voice_true);
            }
            voice = !voice;
            mIvMaterialVoice.setImageResource(voice ? R.drawable.ic_material_voice_true : R.drawable.ic_material_voice_false);
            mIvMaterialVoice.setSelected(voice);
        });
        mBinding.rlMaterialVideo.setOnClickListener(v -> {
            if (mBinding.ivMaterialVideoSuspend.getVisibility() == View.GONE) {
                mBinding.ivMaterialVideoSuspend.setVisibility(View.VISIBLE);
                hidePlayerView();
            }
        });
        mBinding.ivMaterialVideoSuspend.setOnClickListener(view -> {
            if (mVodPlayer != null) {
                if (mBinding.ivMaterialVideoSuspend.isSelected()) {
                    mVodPlayer.pause();
                    mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_stop);
                    mBinding.ivMaterialVideoSuspend.setSelected(false);
                } else {
                    mVodPlayer.resume();
                    mBinding.ivMaterialVideoSuspend.setImageResource(R.drawable.ic_material_play_ing);
                    hidePlayerView();
                    mBinding.ivMaterialVideoSuspend.setSelected(true);
                }
            }
        });
        mPopupWindow.setBackgroundDrawable(null);
        mPopupWindow.showAtLocation(inflate, Gravity.CENTER, 0, 0);
    }


    private void hidePlayerView() {
        if (tHandler != null) {
            tHandler.removeMessages(POP_UPDATEPLAYER);
            tHandler.sendEmptyMessageDelayed(POP_UPDATEPLAYER, 3000);
        }
    }

    public boolean setKeyDown() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            return true;
        }
        return false;
    }
}
